Tutorial: Accessing Variables By Name

Accessing Variables By Name

nes-test has the ability to read debug symbols created by ca65 and cc65, allowing you to access things using the variable names in your code!

This is used in most of the memory Methods implemented in NesEmulator

How can I make it work?

This tool expects files in the same file/format as Mesen. As such, Mesen's Documentation on emulator symbols may help.

In short, when you run ca65 or cc65, pass the --debug-info flag. When you run the linker, pass the --dbgfile argument with the same name you use for the rom. Your link command might look something like this:

cl65 -C config.conf -o mygame.nes --dbgfile mygame.dbg *.o nes.lib

You'll need a modern version of cc65 to make this work - older versions use an incompatible version.

If you are able to see all of your symbols in Mesen, they will also be available in the tool.

Examples

Assembly: In your game, you define a variable named myVariable

myVariable: .res 1
; ...
lda #25
sta myVariable

In your test you can do this:

expect(await emulator.getByteValue('myVariable')).toEqual(25);

C: In your game, you define a variable named myCVariable

unsigned char myCVariable;
// Later, in some function
myCVariable = 25;

In your test, you can do this:

expect(await emulator.getByteValue('myCVariable')).toEqual(25);